package com.chuantou.yuba.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.GiftDataTO;
import com.chuantou.yuba.api.to.GiftTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.ViewHolder;

public class ExchangeHistoryFragment extends PaginationListFragment<GiftDataTO> {
	private ExchangeHistoryAdapter mAdapter;
	private AccountManager mAccountManager;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mAccountManager = AccountManager.getInstance(mContext);
		View view = super.onCreateView(inflater, container, savedInstanceState);
		if (mAdapter == null) {
			mListView.setAdapter(mAdapter = new ExchangeHistoryAdapter());
			startRequest();
		}
		return view;
	}

	@Override
	protected BaseGsonRequest<GiftDataTO> getRequest(Listener<GiftDataTO> listener, ErrorListener errorListener) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		UserTO user = mAccountManager.getUserTO();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		String url = UriHelper.getExchangeHistoryUrl();
		GsonRequest<GiftDataTO> request = new GsonRequest<GiftDataTO>(Method.POST, url, listener, errorListener, params, GiftDataTO.class);
		return request;
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_exchange_history);
	}

	@Override
	public void onResponse(GiftDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				List<GiftTO> gifts = response.getGifts();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					// 保存到本地数据库缓存
					mAdapter.clear();
				}
				mAdapter.add(gifts);
			} else {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	class ExchangeHistoryAdapter extends BaseAdapter {
		private List<GiftTO> mList = new ArrayList<GiftTO>();

		public void add(GiftTO to) {
			this.mList.add(to);
		}

		public void add(List<GiftTO> list) {
			if (list != null && !list.isEmpty()) {
				this.mList.addAll(list);
			}
		}

		public void clear() {
			this.mList.clear();
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.exchange_gift_list_item, null);
			}
			GiftTO gift = mList.get(position);
			NetworkImageView coverView = ViewHolder.get(convertView, R.id.cover);
			BitmapBinder.bindNetImage(mContext, coverView, gift.getCover(), R.drawable.default_image_small, false);
			TextView titleView = ViewHolder.get(convertView, R.id.title);
			titleView.setText(gift.getName());
			TextView costView = ViewHolder.get(convertView, R.id.cost);
			costView.setText(mContext.getString(R.string.gift_cost_format, gift.getCost()));
			TextView sponsorView = ViewHolder.get(convertView, R.id.sponsor);
			sponsorView.setText(mContext.getString(R.string.gift_sponsor_format, gift.getSponsor()));
			return convertView;
		}
	}

}
